package com.hd.tiyu.yuejian.Fragment;



import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hd.tiyu.yuejian.Adapter.HuodongAdapter;
import com.hd.tiyu.yuejian.Adapter.PeixunAdapter;
import com.hd.tiyu.yuejian.JavaBean.Activity;
import com.hd.tiyu.yuejian.JavaBean.Item;
import com.hd.tiyu.yuejian.JavaBean.Train;
import com.hd.tiyu.yuejian.Model.ItemposModel;
import com.hd.tiyu.yuejian.Model.ListsModel;
import com.hd.tiyu.yuejian.R;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/9/14.
 */

public class PeixunFragment extends Fragment {
    private View view;
    private RecyclerView recyclerView;
    private List<ImageView> imageViews = new ArrayList<ImageView>();
    private ImageView temp;
    private List<Item> data;
    private RollPagerView mRollViewPager;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.peixuntab,container,false);

        mRollViewPager =view.findViewById(R.id.roll_view_pager);
        //设置播放时间间隔
        mRollViewPager.setPlayDelay(1000);
        //设置透明度
        mRollViewPager.setAnimationDurtion(500);
        //设置适配器
        mRollViewPager.setAdapter(new TestNormalAdapter());

        //设置指示器（顺序依次）
        //自定义指示器图片
        //设置圆点指示器颜色
        //设置文字指示器
        //隐藏指示器
        //mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
        mRollViewPager.setHintView(new ColorPointHintView(getActivity(), Color.YELLOW,Color.WHITE));
        //mRollViewPager.setHintView(new TextHintView(this));
        //mRollViewPager.setHintView(null);
        initView();
        initData();
        initEvent();
        return view;

    }

    private void initEvent() {

    }

    private void initView() {
        recyclerView=view.findViewById(R.id.re_peixunlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }
    private void initData() {
        ListsModel listsmodel=new ListsModel();
        Call<List<Train>>call=listsmodel.getlistsTrain();
        call.enqueue(new Callback<List<Train>>() {
            @Override
            public void onResponse(Call<List<Train>> call,Response<List<Train>> response) {
                List<Train> train=response.body();
                recyclerView.setAdapter(new PeixunAdapter(getActivity(),train));
            }
            @Override
            public void onFailure(Call<List<Train>> call,Throwable t) {

            }
        });


        ItemposModel itemposModel=new ItemposModel();
        Call<List<Item>> itemcall=itemposModel.getitemps();
        itemcall.enqueue(new Callback<List<Item>>() {
            @Override
            public void onResponse(Call<List<Item>> call, Response<List<Item>> response) {
                data=response.body();
                for(int i=0;i<data.size();i++){
                    temp=new ImageView(getContext());
                    Picasso.with(getActivity()).load("http://172.24.10.175/workout/Uploads/"+data.get(i).getThumb()).into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                            temp.setImageBitmap(bitmap);
                            imageViews.add(temp);

                        }

                        @Override
                        public void onBitmapFailed(Drawable errorDrawable) {

                        }

                        @Override
                        public void onPrepareLoad(Drawable placeHolderDrawable) {

                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<List<Item>> call, Throwable t) {

            }
        });


    }
    private class TestNormalAdapter extends StaticPagerAdapter {
        private int[] imgs = {
                R.drawable.l1,
                R.drawable.l2
        };

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            view.setImageResource(imgs[position]);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }

        @Override
        public int getCount() {
            return imgs.length;
        }
    }

    }

